package com.bidevalution.business.impl;

import com.bidevalution.business.service.BeExpertRecommendCandidateService;
import com.bidevalution.business.service.BeSectionParameterService;
import com.bidevalution.dao.entity.BeExpertRecommendCandidate;
import com.bidevalution.dao.entity.BeSectionExpertInfo;
import com.bidevalution.dao.entity.BeSectionParameter;
import com.bidevalution.dao.mapper.BeExpertRecommendCandidateMapper;
import com.bidevalution.dao.mapper.BeSectionExpertInfoMapper;
import com.qtp.core.model.Response;
import com.qtp.core.util.OConvertUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author 查慧英
 * @date 2019-11-21 15:24
 */
@Service(value = "recommendCandidateService")
public class BeExpertRecommendCandidateServiceImpl implements BeExpertRecommendCandidateService {
    private Logger logger = Logger.getLogger(getClass());
    private static final String SECTION_ID = "sectionId";
    private static final String EXPERT_ID = "expertId";
    private static final String ENTERPRISE_IDS = "enterpriseIds";
    private static final String RANKS = "ranks";
    private static final String COMMA = ",";
    private static final String SEQ_IDS = "seqIds";
    private static final String PARAM_ID = "AdvanceNum";
    @Autowired(required = false)
    BeExpertRecommendCandidateMapper recommendCandidateMapper;
    @Autowired(required = false)
    private BeSectionExpertInfoMapper beSectionExpertInfoMapper;
    @Autowired(required = false)
    BeSectionParameterService beSectionParameterService;
    @Override
    public Response<List<BeExpertRecommendCandidate>> getRecommendCandidateList(Map<String, Object> params) {
        try {
            BeSectionExpertInfo info = getExpertInfo(params);
            String expertId = info.getSeqId();
            String sectionId = OConvertUtils.getString(params.get(SECTION_ID));
            params.put(EXPERT_ID, expertId);
            List<BeExpertRecommendCandidate> list = recommendCandidateMapper.list(params);
            if (list == null || list.isEmpty()) {
                list = new ArrayList<>();
                List<BeExpertRecommendCandidate> entList = recommendCandidateMapper.getRecommendCandidateList(params);
                String[] paramId = new String[]{PARAM_ID};
                List<BeSectionParameter> paramList = beSectionParameterService.queryListByParamIds(sectionId, paramId);
                int advanceNum = entList.size();
                if (paramList != null && !paramList.isEmpty()) {
                    BeSectionParameter parameter = paramList.get(0);
                    advanceNum = OConvertUtils.getInt(parameter.getParamValue(),1);
                }
                if (entList != null && !entList.isEmpty()) {
                    for (int i = 0; i < advanceNum; i++) {
                        BeExpertRecommendCandidate item = entList.get(i);
                        item.setSeqId(UUID.randomUUID().toString());
                        item.setExpertId(expertId);
                        item.setCreateTime(new Date());
                        recommendCandidateMapper.create(item);
                        list.add(item);
                    }
                }
            }
            return Response.ok(list);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return Response.fail("操作失败！");
        }

    }
    private BeSectionExpertInfo getExpertInfo(Map<String,Object> parameters) {
        String loginName = OConvertUtils.getString(parameters.get("loginName"));
        String sectionId = OConvertUtils.getString(parameters.get("sectionId"));
        return beSectionExpertInfoMapper.selectByLoginName(sectionId, loginName);
    }
    @Override
    public Response addOrUpdate(Map<String, Object> params) {
        String seqIds = OConvertUtils.getString(params.get(SEQ_IDS));
        String sectionId = OConvertUtils.getString(params.get(SECTION_ID));
        String expertId = OConvertUtils.getString(params.get(EXPERT_ID));
        String enterpriseIds = OConvertUtils.getString(params.get(ENTERPRISE_IDS));
        String ranks = OConvertUtils.getString(params.get(RANKS));
        String[] idAttr;
        String[] rankAttr;
        String[] enterpriseIdAttr;
        try {
            if (OConvertUtils.isNotEmpty(enterpriseIds)) {
                enterpriseIdAttr = enterpriseIds.split(COMMA);
                rankAttr = ranks.split(COMMA);
                idAttr = seqIds.split(COMMA);
                BeExpertRecommendCandidate item = new BeExpertRecommendCandidate();
                List<BeExpertRecommendCandidate> list = recommendCandidateMapper.list(params);
                if (list != null && !list.isEmpty()) {

                    item.setUpdateTime(new Date());
                    return updateItem(item, idAttr, rankAttr);
                } else {
                    item.setSectionId(sectionId);
                    item.setExpertId(expertId);
                    item.setCreateTime(new Date());
                    return createItem(item, idAttr, enterpriseIdAttr, rankAttr);
                }
            }
            return Response.fail("没有候选人信息");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return Response.fail("操作失败！");
        }
    }
    private Response createItem(BeExpertRecommendCandidate item, String[] idAttr, String[] enterpriseIdAttr, String[] rankAttr) {
        for (int i = 0; i < idAttr.length; i++) {
            item.setSeqId(idAttr[i]);
            item.setEnterpriseId(enterpriseIdAttr[i]);
            item.setRank(OConvertUtils.getInt(rankAttr[i]));
            recommendCandidateMapper.create(item);
        }
        return Response.ok();
    }
    private Response updateItem(BeExpertRecommendCandidate item, String[] idAttr, String[] rankAttr) {
        for (int i = 0; i < idAttr.length; i++) {
            item.setSeqId(idAttr[i]);
            item.setRank(OConvertUtils.getInt(rankAttr[i]));
            recommendCandidateMapper.update(item);
        }
        return Response.ok();
    }
}
